<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Admins extends Allow
{
    public function getindex()
    {
        $request=request();
        //获取需要修改的值
        $name=$request->param('keywords');
        //获取admin_users表用户信息
        $users=Db::table("admin_users")->where('name','like',"%".$name."%")->paginate(5);
    	//加载后台角色模板
    	return $this->fetch("Admins/index",['users'=>$users,'request'=>$request->param()]);
    }

    //添加
    public function getadd(){
        //查询role表
        $role=Db::table('role')->select();
        //加载角色添加模板
        return $this->fetch("Admins/add",['role'=>$role]);
    }

    //执行添加
    public function postinsert(){
        $request=request();
        //获取rid
        $rid=$request->param('rid');
        if(!empty($rid)){
            //获取表单传过来的值
            $data=$request->only(['name','password']);
            //判断传过来的值是否为空
            if(!empty($data['name']) && !empty($data['password'])){
                //执行添加
                if($uid=Db::name('admin_users')->insertGetId($data)){
                    $user_role['uid']=$uid;
                    $user_role['rid']=$rid;
                    Db::table('user_role')->insert($user_role);
                    $this->success("添加成功","/adminsuser/index");
                }else{
                    $this->error("添加失败","/adminsuser/add");
                }
            }else{
                    $this->error("添加的用户不能为空","/adminsuser/add");
            }
        }else{
            $this->error("请问新添加的管理员分配角色","/adminsuser/add");
        }
    }

    //删除
    public function getdelete(){
        $request=request();
        //获取id
        $id=$request->param('id');
        //执行删除
        if(Db::table('admin_users')->where('id',"{$id}")->delete() && Db::table('user_role')->where('uid',"{$id}")->delete()){
            $this->success("删除成功","/adminsuser/index");
        }else{
            $this->error("删除失败","/adminsuser/index");

        }
    }

    //修改
    public function getedit(){
        $request=request();
        //获取id
        $id=$request->param('id');
        //获取需要编辑的数据
        $role=Db::table('admin_users')->where("id","{$id}")->find();
        return $this->fetch("Admins/edit",['role'=>$role]);
    }

    //执行修改
    public function postupdate(){
        $request=request();
        //获取id
        $id=$request->param('id');
        //获取需要修改的数据
        $data=$request->only(['name','password']);
        if(Db::table('admin_users')->where("id","{$id}")->update($data)){
            $this->success("修改成功","/adminsuser/index");
        }else{
            $this->error("修改失败","/adminsuser/edit");
        }
    }

    //分配角色
    public function getrolelist(){
        $request=request();
        //获取id
        $id=$request->param('id');
        //获取管理员名称
        $user=Db::table('admin_users')->where('id',"{$id}")->find();
        //获取所有角色信息
        $role=Db::table('role')->select();
        //获取当前用户所具有的角色信息
        $data=Db::table('user_role')->where("uid","{$id}")->select();
        //获取角色id 存储在数组里
        //遍历
        foreach($data as $v){
            $rids[]=$v['rid'];
        }
        return $this->fetch("Admins/rolelist",['user'=>$user,'role'=>$role,'rids'=>$rids]);
    }

    //保存角色
    public function postsaverole(){
        //获取用户id
        $request=request();
        //获取用户id
        $uid=$request->param('uid');
        //把当前用户已有的角色信息删除掉
      
       
        //判断是否选择了角色
        if(!empty($_POST['rid'])){  
            Db::table("user_role")->where("uid","{$uid}")->delete();
            //获取角色id
            $rid=$_POST['rid'];
            //遍历数组$rid
            foreach($rid as $v){
                $data['uid']=$uid;//用户id
                $data['rid']=$v;//新的角色id

                //执行插入
                Db::table("user_role")->insert($data);
            }
            $this->success("角色分配成功","/adminsuser/index");
            //想user_role插入数据
        }else{
            $this->error("请为管理员分配角色","/adminsuser/index");
        }
    }
}
